module.exports = function () {
    var sequelize = require("../libs/db").sequelize;
    var Access = sequelize.import("../models/iframe_config");
    /**
     * 获取分页数据
     */
    var getItems = async function (params, cb) {
        var Do = async function (cb) {
            try {
                var Result = {};
                var QueryModel = {};
                var CurPage = params.CurPage ? Number(params.CurPage) : 1;
                var PageSize = params.PageSize ? Number(params.PageSize) : 20;
                var Sort = params.Sort ? params.Sort : "ConfigKey|asc";
                Result.CurPage = CurPage;
                Result.PageSize = PageSize;
                QueryModel.ConfigKey = params.ConfigKey;
                var FindFilter = {};

                if (QueryModel.ConfigKey) {
                    FindFilter.ConfigKey = {
                        '$like': '%' + QueryModel.ConfigKey + '%'
                    };
                }

                let data = await Access.findAndCountAll({
                    where: FindFilter,
                    order: [
                        Sort.split('|')
                    ],
                    offset: (CurPage - 1) * PageSize,
                    limit: PageSize,
                    raw:true
                });
                Result.RsCount = data.count;
                Result.DataList = data.rows;
                cb(null, Result);

            } catch (ex) {
                cb(ex);
            }



        }

        if (cb) {
            Do(cb);
        } else {
            return new Promise(function (resolve, reject) {
                Do(function (err, ret) {
                    if (err) {
                        console.log(err);
                        reject(err);
                    } else {
                        resolve(ret);
                    }
                })
            })
        }
    };



    /**
     * 判断新的key是否可用
     * @param {*} oldKey 
     * @param {*} newKey 
     * @param {*} cb 
     */
    var checkIsAble = async function (oldKey, newKey, cb) {
        var Do = async function (cb) {
            var QueryModel = {};
            let count = await Access.count({
                where: {
                    ConfigKey: {
                        '$eq': newKey,
                        '$ne': oldKey
                    }
                }
            });
            if (count > 0) {
                cb(null, false);
            } else {
                cb(null, true);
            }
        }

        if (cb) {
            Do(cb);
        } else {
            return new Promise(function (resolve, reject) {
                Do(function (err, ret) {
                    if (err) {
                        console.log(err);
                        reject(err);
                    } else {
                        resolve(ret);
                    }
                })
            })
        }
    }


    return {
        Access: Access,
        getItems: getItems,
        checkIsAble: checkIsAble
    }
}();